#include "settingpwsswdwidget.h"
#include "ui_settingpwsswdwidget.h"
#include "qdebug.h"
#include "Core/PasswdTool/passwdctrl.h"
#include "qtimer.h"

SettingPwsswdWidget::SettingPwsswdWidget(OpenType open_type, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::SettingPwsswdWidget),
    m_open_type(ValidatePW),
    m_input_str_filter(false),
    m_no_notice_flag(false)
{
    ui->setupUi(this);

    m_open_type = open_type;
    this->setFixedSize(this->width(),this->height());
    this->setWindowFlags(windowFlags()&~Qt::WindowCloseButtonHint&~Qt::WindowContextHelpButtonHint);
    ui->message_lab->setText(QString(""));
    NoNoticeChangeInputText(QString(""));
    qDebug() << QString(" SettingPwsswdWidget::SettingPwsswdWidget %1").arg(this->windowTitle());

    if(m_open_type == ValidatePW)
    {
        // 取消两个关闭按钮
        this->ui->cancel_btn->setEnabled(false);
        this->ui->enter_btn->setText("验证");
    }
    else if(m_open_type == ChangePW)
    {
        this->ui->cancel_btn->setEnabled(true);
        this->ui->enter_btn->setText("储存");
    }
}

SettingPwsswdWidget *SettingPwsswdWidget::TryNewSettingPwsswdWidget(OpenType open_type, QWidget *parent)
{
    auto p = new SettingPwsswdWidget(open_type,parent);
    if(p->CheckCanShow())
    {
        return p;
    }
    else
    {
        delete p;
        return NULL;
    }
}

SettingPwsswdWidget::~SettingPwsswdWidget()
{
    delete ui;
}

void SettingPwsswdWidget::on_enter_btn_clicked()
{
    // 验证不通过返回
    if(!m_input_str_filter)
    {
        return;
    }

    QString str = ui->input_passwd_lad->text();
    qDebug()<<str;

    NoNoticeChangeInputText(QString(""));


    if(m_open_type == ValidatePW)
    {
        QString pw = PasswdCtrl::ReadPasswd();
        if(pw.compare(str) == 0)
        {
            ui->message_lab->setText("验证成功，即将进入主界面");
            ui->cancel_btn->setEnabled(true);
            QTimer *timer = new QTimer(this);
            timer->start(1 * 1000);
            connect(timer,&QTimer::timeout,this,[=]()
            {
                qDebug()<<QString("___ValidatePW");
                timer->stop();
                this->close();
            });
        }
        else
        {
            ui->message_lab->setText("密码错误，请重试");
        }
    }
    else if(m_open_type == ChangePW)
    {
        PasswdCtrl::WritePasswd(str);
        if(PasswdCtrl::ReadPasswd().compare(str) == 0)
        {
            ui->message_lab->setText("储存成功");
        }
        else
        {
            ui->message_lab->setText("储存失败");
        }
    }

}

void SettingPwsswdWidget::on_cancel_btn_clicked()
{
    this->close();
}

void SettingPwsswdWidget::on_input_passwd_lad_textChanged(const QString &input_pw)
{
    // 执行不提醒更改值期间 不提示
    if(m_no_notice_flag){  return; }

    m_input_str_filter = false;
    if(input_pw.isNull())
    {
        ui->message_lab->setText("输入不能为空");
    }
    else if(input_pw.isEmpty())
    {
        ui->message_lab->setText("输入不能为空");
    }
    else if(input_pw.size() != 6)
    {
        ui->message_lab->setText(QString("密码必须是六位数").arg(QString("6")));
    }
    else
    {
        m_input_str_filter = true;
        ui->message_lab->setText("");
    }
}

bool SettingPwsswdWidget::CheckCanShow()
{
    if(m_open_type == ValidatePW)
    {
        // 有密码不能够打开
        QString pw = PasswdCtrl::ReadPasswd();
        return (!pw.isEmpty());
    }
    else
    {
        return true;
    }
}

void SettingPwsswdWidget::NoNoticeChangeInputText(QString &val)
{
    m_no_notice_flag = true;
    ui->input_passwd_lad->setText(val);
    m_no_notice_flag = false;
}
